/*—————————————————————————————————————————————————————————— 公共hooks文件 ——————————————————————————————————————————————————————————*/

// redux再次更新hooks所需依赖
import { useSelector, useDispatch, TypedUseSelectorHook } from 'react-redux' // 引入redux的hook
import { AppDispatch, RootState } from '../store/store' // 引入封装好后的对应的ts类型判断

// 请求接口再次封装公共hooks所需依赖
// --------------------------------------------------------------------------------------------------------
import useApi from '@/api/interceptor'
import { Request } from '@/api/interface'

// 导出hooks区域
// ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
/**
 * @针对使用redux官方hook时ts类别报错的封装处理
 * @导出使用即可和原版没有区别的使用方法
 */
export const useAppDispatch: () => AppDispatch = useDispatch
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector

/**
 * @请求接口再次封装公共hooks
 */
// --------------------------------------------------------------------------------------------------------
export const useRequest: Request = (customOptions = {}) => {
  // 使用请求api的hook
  const api = useApi(customOptions)

  return {
    // get请求
    // =========================================================================================
    get: (url, params, config) =>
      api({
        method: 'get',
        url,
        params,
        ...config
      }),

    // post请求
    // =========================================================================================
    post: (url, params, config) =>
      api({
        method: 'post',
        url,
        data: params,
        ...config
      }),

    // put请求
    // =========================================================================================
    put: (url, params, config) =>
      api({
        method: 'put',
        url,
        data: params,
        ...config
      }),

    // delete请求
    // =========================================================================================
    delete: (url, params, config) =>
      api({
        method: 'delete',
        url,
        data: params,
        ...config
      }),

    // 下载函数
    // =========================================================================================
    download: (url, params, config) =>
      api({
        method: 'get',
        url,
        params,
        responseType: 'blob',
        ...config
      })
  }
}
